package com.askyjt.videosearch.controller;


import com.askyjt.videosearch.common.constant.Constant;
import com.askyjt.videosearch.entity.Video;
import com.askyjt.videosearch.service.VideoService;
import com.askyjt.videosearch.util.MinioUtil;
import com.askyjt.videosearch.util.Result;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.io.File;
import java.io.InputStream;
import java.util.Arrays;
import java.util.UUID;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author yangjintao
 * @since 2020-11-02
 */
@Slf4j
@RestController
@RequestMapping("admin/video")
public class VideoController {

    @Resource
    private VideoService videoService;

    @Resource
    private MinioUtil minioUtil;

    @PostMapping("/uploadVideo/v1")
    public Result<Boolean> uploadVideo(@RequestParam("description") String description,
                                       @RequestParam("file") MultipartFile file) {
        if (file.isEmpty() || file.getSize() == 0) {
            return Result.error("error", "文件不能为空！");
        }
        try {
            String fileName;
            if ((fileName = file.getOriginalFilename()) != null) {
                String newName = Constant.MINIO_VIDEO_PREFIX + UUID.randomUUID().toString().replaceAll("-", "")
                        + fileName.substring(fileName.lastIndexOf("."));
                File saveFile = new File(Constant.LOCAL_SAVE_PATH + newName);
                Video video = new Video().setVideoUri(saveFile.getAbsolutePath());
                file.transferTo(saveFile);
                if (description != null) {
                    video.setVideoDescription(description);
                    String name = fileName.substring(0,fileName.lastIndexOf("."));
                    log.info("name : " + name);
                    video.setVideoName(name);
                }
                Boolean saveResult = videoService.saveVideo(video);
                if (saveResult) {
                    log.info("成功上传视频: " + newName);
                    return Result.success();
                }
                return Result.error();

            }
            return Result.error("error", "文件名不能为空！");
        } catch (Exception e) {
            e.printStackTrace();
            return Result.error("error", "上传失败");
        }
    }

    @PostMapping("/uploadVideo/v2")
    public Result<Boolean> uploadVideo1(@RequestParam("description") String description,
                                       @RequestParam("file") MultipartFile file) {
        if (file.isEmpty() || file.getSize() == 0) {
            return Result.error("error", "文件不能为空！");
        }
        try {
            String fileName;
            if ((fileName = file.getOriginalFilename()) != null) {
                String newName = Constant.MINIO_VIDEO_PREFIX + UUID.randomUUID().toString().replaceAll("-", "")
                        + fileName.substring(fileName.lastIndexOf("."));
                InputStream inputStream = file.getInputStream();
                minioUtil.putObject(Constant.MINIO_BUCKET_VIDEO, newName, inputStream, file.getContentType());
                inputStream.close();
                String url = minioUtil.getObjectUrl(Constant.MINIO_BUCKET_VIDEO, newName);
                Video video = new Video().setVideoUri(url);
                if (description != null) {
                    video.setVideoDescription(description);
                    String name = fileName.substring(0,fileName.lastIndexOf("."));
                    log.info("upload name : " + name);
                    video.setVideoName(name);
                }
                Boolean saveResult = videoService.saveVideo(video);
                if (saveResult) {
                    log.info("成功上传视频: " + newName);
                    return Result.success();
                }
                return Result.error();

            }
            return Result.error("error", "文件名不能为空！");
        } catch (Exception e) {
            e.printStackTrace();
            return Result.error("error", "上传失败");
        }
    }

    @PostMapping("/deleteVideo")
    public Result<Boolean> deleteVideo(Long[] ids) {
        try {
            videoService.deleteVideoById(Arrays.asList(ids));
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        return Result.error("error","删除失败！");
    }
}

